<template>
	<view class="container">
    <uni-section title="个人信息" type="line">
      <uni-list>
        <uni-list-item title="名称" :rightText="userInfo.userName" />
      </uni-list>
      <uni-list>
        <uni-list-item title="手机" :rightText="userInfo.phone" />
      </uni-list>
    </uni-section>
    <view class="uni-padding-wrap uni-common-mt">
      <button type="warn" @click="logout()">注销登录</button>
    </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
        userInfo: {
          userName: "",
          phone: ""
        }
			}
		},
    onShow() {
      this.validAndSetUserInfo();
    },
		methods: {
      logout() {
        uni.request({
          url: '/sso/logout',
          success: (res) => {
            if(res.statusCode === 200 || res.statusCode === 401) {

              uni.showModal({
                content: "请登录",
                showCancel: false
              });
              setTimeout(() => {
                uni.navigateTo({
                  url: '/pages/login/login'
                });
              }, 500)

            } else {
              uni.showToast({
                title: '接口调用失败',
                icon: 'none'
              });
            }
          }
        })
      },
      async validAndSetUserInfo() {
        uni.request({
          url: '/api/sysUser/me',
          data: {},
          success: (res) => {
            console.log(res.statusCode)
            if (res.statusCode === 200) {
              console.log(res.data.data);
              let data =  res.data.data;
              this.userInfo.userName = data.realName;
              this.userInfo.phone = data.mobile;
            } else if (res.statusCode === 401) {
              uni.showModal({
                content: "请登录",
                showCancel: false
              });
              setTimeout(() => {
                uni.navigateTo({
                  url: '/pages/login/login'
                });
              }, 500)
            } else {
              uni.showToast({
                title: '用户信息获取失败',
                icon: 'none'
              });
            }


          }
        });
      }
		}
	}
</script>

<style>

</style>
